{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "adaptive-penny",
   "metadata": {},
   "source": [
    "# Problem 14.13 Thermodynamic Cycle Calculation Using a High-Precision EOS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "speaking-pontiac",
   "metadata": {},
   "source": [
    "A thermodynamic cycle with water as the working fluid consists of the following steps:\n",
    "    \n",
    "* Constant-pressure heating to P1 = 100 bar and T1 = 350 °C\n",
    "* Isentropic expansion of the gas in a turbine to P2 = 1 bar (reversible; efficiency = 100%)\n",
    "* Constant pressure condensation\n",
    "* Isentropic compression of the liquid to P4 = 100 bar\n",
    "\n",
    "What is the thermal efficiency of the process?\n",
    "\n",
    "$$ \\eta_{th}  = -\\frac{P_{12} + P_{34}}{Q_{41}}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "neither-topic",
   "metadata": {},
   "source": [
    "## Solution\n",
    "\n",
    "This is quite straightforward."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "valuable-spending",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(44969.97634439414,\n",
       " -31180.343551697508,\n",
       " -13975.281899345828,\n",
       " 185.64910664919353)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "from thermo import FlashPureVLS, IAPWS95Liquid, IAPWS95Gas, iapws_constants, iapws_correlations\n",
    "from scipy.integrate import quad\n",
    "import numpy as np\n",
    "\n",
    "T1 = 350 + 273.15\n",
    "P1 = 100*1e5\n",
    "P2 = 1e5\n",
    "# Entropy conserved in step 2 as well\n",
    "VF3 = 0\n",
    "P3 = P2\n",
    "\n",
    "P4 = P1\n",
    "# entropy conserved in step 5 as well\n",
    "\n",
    "\n",
    "liquid = IAPWS95Liquid(T=T1, P=P1, zs=[1])\n",
    "gas = IAPWS95Gas(T=T1, P=P1, zs=[1])\n",
    "flasher = FlashPureVLS(iapws_constants, iapws_correlations, gas, [liquid], [])\n",
    "\n",
    "\n",
    "stage_1 = flasher.flash(P=P1, T=T1)\n",
    "stage_2 = flasher.flash(P=P2, S=stage_1.S())\n",
    "stage_3 = flasher.flash(VF=VF3, P=P3)\n",
    "stage_4 = flasher.flash(P=P4, S=stage_3.S())\n",
    "\n",
    "expander_duty = stage_2.H() - stage_1.H()\n",
    "pump_duty = stage_4.H() - stage_3.H()\n",
    "heating_duty = stage_1.H() - stage_4.H()\n",
    "cooling_duty = stage_3.H() - stage_2.H()\n",
    "heating_duty, cooling_duty, expander_duty, pump_duty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "parliamentary-indicator",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-9.094947017729282e-13"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# it is easy to check the cycle converged\n",
    "cycle_error = sum([heating_duty, cooling_duty, expander_duty, pump_duty])\n",
    "cycle_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "handy-graduate",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The thermal efficiency is 31.08 %\n"
     ]
    }
   ],
   "source": [
    "# Not quite sure what definition is being suggested by the textbook\n",
    "eta_th = -expander_duty/heating_duty\n",
    "print(f'The thermal efficiency is {eta_th*100:.2f} %')"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
